﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;

namespace fund
{
    public static class JArrayConverter
    {
        public static DataTable jsonToTable(JArray arr)
        {
            //    JObject line = (JObject)arr[0];
            //    DataTable td = new DataTable();
            //    //设置列
            //    foreach (JProperty pro in line.Children())
            //    {
            //        DataColumn dc = new DataColumn();
            //        dc.ColumnName = pro.Name;
            //        td.Columns.Add(dc);
            //    }
            //  JObject line = (JObject)arr[0];
            DataTable td = new DataTable();
            //设置列
            for (int i = 0; i < (arr[0] as JArray).Count; i++)//第一行数组的长度
            {
                DataColumn dc = new DataColumn();
                dc.ColumnName = "Col" + i.ToString();
                td.Columns.Add(dc);
            }
            td.AcceptChanges();
            //增加行
            for (int i = 0; i < arr.Count; i++)
            {
                DataRow dr = td.NewRow();
                for (int r = 0; r < td.Columns.Count; r++)
                {
                    // dr[r] = arr[i][td.Columns[r].ToString()];
                    dr[r] = arr[i][r];
                }
                td.Rows.Add(dr);
            }
            td.AcceptChanges();
            return td;
        }

        public static List<Fund> ArrayToList_JZ(JArray arr)
        {
            List<Fund> funds = new List<Fund>();

            //增加行
            for (int i = 0; i < arr.Count; i++)
            {
                Fund fund = new Fund();
                fund.FundNo = Convert.ToInt32(arr[i][14]);
                fund.FundCode = Convert.ToString(arr[i][0]);
                fund.FundName = Convert.ToString(arr[i][1]);
                fund.dwjz = arr[i][3].ToString() == "" ? -1 : Convert.ToDecimal(arr[i][3]);
                fund.ljjz = arr[i][4].ToString() == "" ? -1 : Convert.ToDecimal(arr[i][4]);
                fund.GrowthRate = string.IsNullOrEmpty(arr[i][8].ToString()) ? "-" : arr[i][8].ToString() + "%";
                fund.sgStatus = Convert.ToString(arr[i][9]);
                fund.shStatus = Convert.ToString(arr[i][10]);
                fund.IsCanBuy = Convert.ToString(arr[i][12]);
                fund.Commission = Convert.ToString(arr[i][18]);
                funds.Add(fund);
            }

            return funds;
        }

        public static List<Fund> ArrayToList_PM(JArray arr)
        {
            List<Fund> funds = new List<Fund>();

            //增加行
            for (int i = 0; i < arr.Count; i++)
            {
                string[] fundPM = Convert.ToString(arr[i]).Split(',');
                Fund fund = new Fund();
                fund.FundCode = Convert.ToString(fundPM[0]);
                fund.FundName = Convert.ToString(fundPM[1]);

                fund.ColGrow1W = Convert.ToString(fundPM[7]) == "" ? "-" : (Math.Round(Convert.ToDecimal(fundPM[7]), 2).ToString() + "%");
                fund.ColGrow1M = Convert.ToString(fundPM[8]) == "" ? "-" : (Math.Round(Convert.ToDecimal(fundPM[8]), 2).ToString() + "%");
                fund.ColGrow3M = Convert.ToString(fundPM[9]) == "" ? "-" : (Math.Round(Convert.ToDecimal(fundPM[9]), 2).ToString() + "%");
                fund.ColGrow6M = Convert.ToString(fundPM[10]) == "" ? "-" : (Math.Round(Convert.ToDecimal(fundPM[10]), 2).ToString() + "%");
                fund.ColGrow1Y = Convert.ToString(fundPM[11]) == "" ? "-" : (Math.Round(Convert.ToDecimal(fundPM[11]), 2).ToString() + "%");
                fund.ColGrow2Y = Convert.ToString(fundPM[12]) == "" ? "-" : (Math.Round(Convert.ToDecimal(fundPM[12]), 2).ToString() + "%");
                fund.ColGrow3Y = Convert.ToString(fundPM[13]) == "" ? "-" : (Math.Round(Convert.ToDecimal(fundPM[13]), 2).ToString() + "%");
                fund.ColGrow = Convert.ToString(fundPM[14]) == "" ? "-" : (Math.Round(Convert.ToDecimal(fundPM[14]), 2).ToString() + "%");
                fund.ColGrowFrom6 = Convert.ToString(fundPM[18]) == "" ? "-" : (Math.Round(Convert.ToDecimal(fundPM[18]), 2).ToString() + "%");

                funds.Add(fund);
            }

            return funds;
        }
    }
}